<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="jakarta.servlet.http.Cookie" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="Dao.BaseDao" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<html>
<head>
    <title>个人中心</title>
    <link rel="stylesheet" href="table.css">
    <!--检测是否非法访问-->
    <%
        Cookie cookie[] = request.getCookies();
        String uname = null;
        String uid = null;
        for (int i = 0; i < cookie.length; i++) {
            if (cookie[i].getName().equals("uname")) {
                uname = cookie[i].getValue();
            }
            if (cookie[i].getName().equals("uid")) {
                uid = cookie[i].getValue();
            }
        }
        if (uname == null || uname.equals("")) {
            out.println("<script>alert('非法访问，正在返回登录界面')</script>");
            response.setHeader("refresh", "0;URL=login.jsp");
        }
        if (uid == null || uid.equals("")) {
            out.println("<script>alert('非法访问，正在返回登录界面')</script>");
            response.setHeader("refresh", "0;URL=login.jsp");
        }
    %>
    <!--查询写过几条信息-->
    <%
        int num = 0;
        try {
            String SQL_SELECT= "select count(author) as num from talk_with_friends.message where author = '" + uname + "';";
            //连接数据库，获得连接对象
            Connection conn = BaseDao.getConnection();
            //搭建执行环境
            Statement statement = conn.createStatement();
            //执行sql的查询语句，得到结果集
            ResultSet result = statement.executeQuery(SQL_SELECT);
            while (result.next()) {
                num= result.getInt("num");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    %>
    <!--查询写过几条消息-->
    <%
        int writeChatNum = 0;
        try {
            String SQL_SELECT= "select count(author) as num from talk_with_friends.message where author = '" + uname + "';";
            //连接数据库，获得连接对象
            Connection conn = BaseDao.getConnection();
            //搭建执行环境
            Statement statement = conn.createStatement();
            //执行sql的查询语句，得到结果集
            ResultSet result = statement.executeQuery(SQL_SELECT);
            while (result.next()) {
                num= result.getInt("num");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    %>
    <!--查询收到几条消息-->

</head>
<body>
<div align="right">
    <text>用户：<%=uname%></text>
    <a href="logout.jsp">退出</a>
</div>
<table>
    <tr>
        <td>用户id</td>
        <td><%=uid%></td>
    </tr>
    <tr>
        <td>用户名</td>
        <td><%=uname%></td>
    </tr>
    <tr>
        <td>已留言</td>
        <td><%=num%></td>
        <td>条</td>
    </tr>
    <tr>
        <td>已发出</td>
        <td><%=num%></td>
        <td>条消息</td>
        <td><a href="">查看详情</a></td>
    </tr>
    <tr>
        <td>收到了</td>
        <td><%=num%></td>
        <td>条消息</td>
        <td><a href="">查看详情</a></td>
    </tr>
    <tr>
        <td>修改个人信息</td>
        <form action="changeuser.jsp">
            <input type ="hidden" name="choose" value="修改">
            <td><input type="submit" value="修改用户名" name="type"></td>
            <td><input type="submit" value="修改密码" name="type"></td>
        </form>
    </tr>
    <tr>
        <td colspan="4"><a href="message.jsp">返回留言界面</a></td>
    </tr>
</table>
</body>